﻿import versionInfo
from optparse import OptionParser
from loadstone import textparser,dbtools,db
import codecs
import time
from utils import *
try:
	import psyco
	psyco.full()
except ImportError:
	pass

opts = OptionParser(usage = _("usage: %prog [options] FILENAME"),version=versionInfo.version,description=_("Checks a Loadstone database file for syntax errors,  UTF8 compliance and removes unwanted code left behind by text processors such as MS Word. Aditionally you can remove entries  that are flagged for deletion on the Point Share Exchange website (after you first have uploaded these deletions), remove duplicate entries by position and/or name, or 'thin out' a crowded database by declaring points of close proximity as duplicates."))
opts.add_option("-o","--output",metavar="FILE",help=_("Output file name"))
opts.add_option("-d","--skip-deleted",action="store_true",default=False,help=_("Skip points flagged for deletion"))
opts.add_option("-p","--reduce-close",type="float",metavar="DISTANCE",help=_("Remove points which positions are within specified DISTANCE in meters"))
opts.add_option("-n","--reduce-close-names",type="float",metavar="DISTANCE",help=_("Remove points which positions are within specified DISTANCE in meters and names are equal"))
opts.add_option("-s","--sort-by",type="choice",action="append",metavar="FIELD",choices=db.PointRecord.fields,help=_('Add specified field to the ordered list of fields to sort result by. Allowed field names are: "name","latitude","longitude","accuracy","satellites","priority","userid","id"'))
(options, args) = opts.parse_args()
if len(args) < 1:
	opts.error(_("Database text file is not specified"))
initStdout()
for filename in args:
	try:
		input=codecs.open(filename,"r","utf-8",errors="ignore")
	except Exception,e:
		print _("Error: %s")%str(e)
		exit(2)
	parser=textparser.parseTextFile(filename)
	for line,errtype,errstr in parser.errors:
		print "%s | %d | %s: %s"%(filename,line,errtype,errstr)
	print _("%d points loaded")%len(parser.db.point)
	if options.skip_deleted:
		print _("removed %d records flagged for deletion")%(dbtools.excludeDeletedRecords(parser.db))
	if options.reduce_close:
		print _("removed %d records with duplicate positions")%dbtools.reduceClosePoints(parser.db,options.reduce_close)
	if options.reduce_close_names:
		print _("removed %d records with duplicate names")%dbtools.reduceClosePoints(parser.db,options.reduce_close_names,nameMustMatch=True)
	print _("%d points left")%len(parser.db.point)
	if options.output:
		if options.sort_by and len(options.sort_by)>0:
			parser.db.point.sort(key=lambda k:[k[n] for n in options.sort_by])
		with codecs.open(options.output,"wb","utf-8") as output:
			parser.db.writeToBuffer(output)
